package com.dongdongshop.sendmessage;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@RocketMQMessageListener(topic = "get_message",consumerGroup = "my_mq")
public class Sample implements RocketMQListener<String> {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void onMessage(String s) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", "LTAI4GCbdwnjCGwQj9Z3tr3N",
                "27ih4pc1W1tqey9X4mmCy1JUotAX7L");
        /** use STS Token
         DefaultProfile profile = DefaultProfile.getProfile(
         "<your-region-id>",           // The region ID
         "<your-access-key-id>",       // The AccessKey ID of the RAM account
         "<your-access-key-secret>",   // The AccessKey Secret of the RAM account
         "<your-sts-token>");          // STS Token
         **/
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        // 调用al大于发的域名
        request.setSysDomain("dysmsapi.aliyuncs.com");
        // 版本号
        request.setSysVersion("2017-05-25");
        // 发送短信功能
        request.setSysAction("SendSms");
        // 要发送的手机号
        request.putQueryParameter("PhoneNumbers", ""+s+"");
        // 签名
        request.putQueryParameter("SignName", "东科创想");
        // 模板ID
        request.putQueryParameter("TemplateCode", "SMS_162522027");
        // 随机验证码
        String numeric = RandomStringUtils.randomNumeric(6);
        request.putQueryParameter("TemplateParam","{\"sms_code\":\""+numeric+"\"}");
        //
        redisTemplate.opsForValue().set(s,numeric);
        redisTemplate.expire(s,1, TimeUnit.MINUTES);
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

   /* public static void main(String[] args)  {
        DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", "LTAI4GCbdwnjCGwQj9Z3tr3N",
                "27ih4pc1W1tqey9X4mmCy1JUotAX7L");
        *//** use STS Token
         DefaultProfile profile = DefaultProfile.getProfile(
         "<your-region-id>",           // The region ID
         "<your-access-key-id>",       // The AccessKey ID of the RAM account
         "<your-access-key-secret>",   // The AccessKey Secret of the RAM account
         "<your-sts-token>");          // STS Token
         **//*
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        // 调用al大于发的域名
        request.setSysDomain("dysmsapi.aliyuncs.com");
        // 版本号
        request.setSysVersion("2017-05-25");
        // 发送短信功能
        request.setSysAction("SendSms");
        // 要发送的手机号
        request.putQueryParameter("PhoneNumbers", "15762133028");
        // 签名
        request.putQueryParameter("SignName", "东科创想");
        // 模板ID
        request.putQueryParameter("TemplateCode", "SMS_162522027");
        request.putQueryParameter("TemplateParam","{\"sms_code\":\"666\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }*/
}
